﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ForzaGT3.Data;

namespace ForzaGT3.Areas.Admin.Controllers
{
    public partial class ContactController : Controller
    {
        //
        // GET: /Admin/Contact/

        public virtual ActionResult Index()
        {
            // Get paging values from the query string
            int pageNum = CustomGrid.QueryParams.getPageNumber("data", Request.QueryString);
            int pageSize = CustomGrid.QueryParams.getPageSize("data", Request.QueryString);

            // Get sorting values from query string
            string sort = CustomGrid.QueryParams.getSortField("data", Request.QueryString, new Series(), "CreatedDate");
            string stype = CustomGrid.QueryParams.getSortType("data", Request.QueryString, "desc");

            // Get filter where conditions and values from the query string            
            List<object> whereValues = new List<object>();
            string whereConditions = "";
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Name", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Email", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Phone", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Website", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Company", Request.QueryString, whereValues);

            // Get data from database
            int totalRecords;
            IQueryable<Contact> data = Contact.AdminGetAll(whereConditions, whereValues, sort, stype, pageNum, pageSize, out totalRecords);

            // Count the total number of records
            ViewBag.totalRecords = totalRecords;
            ViewBag.CurrentPageName = "Contact";

            return View(data.ToList());
        }

        public virtual ActionResult Details(Guid Id)
        {
            Contact data = Contact.GetById(Id);
            return View(data);
        }

        public virtual ActionResult Delete(Guid Id)
        {
            Contact data = Contact.GetById(Id);
            if (data != null)
            {
                data.IsDeleted = true;
                data.Update();
            }
            return RedirectToAction(MVC.Admin.Contact.Index());
        }

    }
}
